neat-contract
yet another one contract library, data flow focused
If contract is satisfied, returns value, otherwise, throwing TypeError
exception.
Well suited for promises as well.
Install
npm install --save neat-contract
Usage
Basic
import contract from 'neat-contract';
contract('input', String, 'unicorns');
contract('input', String, 2);
contract('input', [String, Array], 'unicorns');
contract('input', [String, Array], ['unicorns']);
contract('input', [String, Array], 2);
contract('input', String)('unicorns');
contract('input', String)(2);
contract(input, Array, ['unicorns', 'cats']);
contract(input, Array, 2);
['unicorns', 'cats'].forEach(contract('input[item]', String));
['unicorns', 2].forEach(contract('input[item]', String));
Data-flow
Sync data-flow
import R from 'ramda';
import contract from 'neat-contract';
const prefixEslintPlugin = R.pipe(
contract('pluginName', String),
R.concat('eslint-plugin-')
);
prefixEslintPlugin('import');
prefixEslintPlugin(true);
prefixEslintPlugin(/reg/);
Async data-flow
import R from 'ramda';
import contract from 'neat-contract';
const toPromise = Promise.resolve.bind(Promise);
const _log = console.log.bind(console);
const _throw = err => { throw err; };
const prefixEslintPluginAsync = R.pipeP(toPromise,
contract('pluginName', String),
R.concat('eslint-plugin-')
);
prefixEslintPluginAsync('import').then(_log);
prefixEslintPluginAsync(true).catch(_throw);
prefixEslintPluginAsync(/reg/).catch(_throw);
Arrays in data-flow
import R from 'ramda';
import contract from 'neat-contract';
const prefixArrayOfEslintPlugins = R.pipe(
R.pipe(
contract('plugins', Array),
R.map(contract('plugins[item]', String))
),
R.map(R.concat('eslint-plugin-'))
);
prefixArrayOfEslintPlugins(['import', 'export']);
prefixArrayOfEslintPlugins(2);
prefixArrayOfEslintPlugins(['unicorns', 2]);
Contracting arrays in asynchronous functions goes, the same way as in async example from above.
API
contract(name, ctor, param)
// contract :: String -> Constructor -> a -> a|throw new TypeError
name
Required
Type: String
Parameter name.
ctor
Required
Type: Constructor
Example: String
, Number
or Function
Parameter Constructor for type check
param
Required
Type: any
Actual parameter itself to type check with Constructor
.
License
MIT © Vladimir Starkov